package com.angel.adapter;

import java.util.ArrayList;

import com.angel.activity.R;
import com.angel.util.AsyncDownload;
import com.angel.util.BaseUtil;
import com.angel.util.HttpUtil;
import com.angel.util.AsyncDownload.CallBack;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
/*用于加载ViewPager适配器*/
public class MyPagerAdapter extends PagerAdapter {
	private Activity activity;
	private ArrayList<String> urlList;
	private LayoutInflater inflater;
	public MyPagerAdapter(Activity activity,ArrayList<String> urlList) {
		this.activity = activity;
		this.urlList=urlList;
		inflater=activity.getLayoutInflater();
	}

	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);

	}

	@Override
	public void finishUpdate(View arg0) {

	}

	@Override
	public int getCount() {
		return urlList.size();
	}

	@Override
	public Object instantiateItem(ViewGroup arg0, int arg1) {
		View v=inflater.inflate(R.layout.detail_big_img_item, null);
		final ProgressBar p=(ProgressBar)v.findViewById(R.id.detail_img_progress);
		ImageView iv = (ImageView)v.findViewById(R.id.datil_big_imgid);
		Log.i("mypageradapter","iv"+iv);
		/*实现异步下载*/
		AsyncDownload async=AsyncDownload.getInstance();
		String url=BaseUtil.getPicUrl(activity, urlList.get(arg1));
		Log.i("","bigimg url:"+url);
		Drawable drw=async.imageDown(iv, url, new CallBack(){
			@Override
			public void imgDown(ImageView img, Drawable drawable) {
				img.setImageDrawable(drawable);
				if(p.getVisibility()!=View.GONE)
				  p.setVisibility(View.GONE);
			}
		});
		if(drw!=null)
			{
			if(p.getVisibility()!=View.GONE)
				p.setVisibility(View.GONE);
				iv.setImageDrawable(drw);
			}
		((ViewPager) arg0).addView(v);
		return v;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == (arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

}
